//
// Created by 15255 on 25-7-11.
//

#ifndef LOG_SYSTEM_H
#define LOG_SYSTEM_H

#include "file_system.h"
#include "usb_cmd.h"
#include "variable_support.h"

//user define

#define MAX_PARAM_NUM 50
#define MAX_MONITOR_VAR_NUM 50

typedef enum{
	LED_L_INIT_STATE,
	LED_L_FREQ,
	LED_R_INIT_STATE,
	LED_R_FREQ,
	NOW_NORMAL_LOG_ID,
	NOW_FLIGHT_LOG_ID,
	IMU2_ACCEL_X_OFFSET,
	IMU2_ACCEL_Y_OFFSET,
	IMU2_ACCEL_Z_OFFSET,
	IMU2_GYRO_X_OFFSET,
	IMU2_GYRO_Y_OFFSET,
	IMU2_GYRO_Z_OFFSET,
	IMU1_ACCEL_X_OFFSET,
	IMU1_ACCEL_Y_OFFSET,
	IMU1_ACCEL_Z_OFFSET,
	IMU1_GYRO_X_OFFSET,
	IMU1_GYRO_Y_OFFSET,
	IMU1_GYRO_Z_OFFSET,
	TEST_REGISTER
}Register_Var_Name;


typedef enum {
    IMU_MIX_ACCEL_X,
    IMU_MIX_ACCEL_Y,
    IMU_MIX_ACCEL_Z,
    IMU_MIX_GYRO_X,
    IMU_MIX_GYRO_Y,
    IMU_MIX_GYRO_Z,
    DART_PITCH,
    DART_YAW,
    DART_ROLL,
    TEST_MONITOR
}Monitor_Var_Name;

//user define

typedef enum{
	LOG_INFO,
	LOG_WARNING,
	LOG_ERROR
}Log_Info_State;


typedef struct{
	Variable_TYPE type;
	Register_Var_Name reg_var_name;
	uint32_t ram_addr;
}Variable_pack;

typedef struct{
	uint16_t now_num;
	Variable_pack pack[MAX_PARAM_NUM];
    uint16_t index_to_offset[MAX_PARAM_NUM];
}Setting_variables;

void REGISTER_PARAM(uint32_t var_ram_addr, Variable_TYPE var_type, Register_Var_Name reg_name);
void UPDATE_PARAM(Register_Var_Name reg_name, uint16_t index, Variable_TYPE type, uint8_t* raw_data);
uint16_t get_setting_index_offset(uint16_t index);
extern Setting_variables setting_variables;

typedef struct{
	Variable_TYPE type;
	Monitor_Var_Name mon_var_name;
	uint32_t ram_addr;
}Monitor_pack;

typedef struct{
	uint16_t now_num;
	Monitor_pack monitor_vars[MAX_MONITOR_VAR_NUM];
    uint16_t index_to_offset[MAX_MONITOR_VAR_NUM];
    bool which_print[MAX_MONITOR_VAR_NUM];
}Monitor_Var;

void MONITOR_VARIABLE(uint32_t var_ram_addr, Variable_TYPE var_type, Monitor_Var_Name reg_name);
extern Monitor_Var monitor_var;
uint16_t get_monitor_index_offset(uint16_t index);


typedef enum{
    WAIT_FILE_SYSTEM_IDLE,
    CHECK_LOG_SYSTEM,
		WRITE_INIT_PARAM,
		PARAM_INIT,
    LOG_SYSTEM_IDLE
}Log_system_State;

typedef struct{
    uint8_t send_buf[256];
    Log_system_State log_system_state;
    Log_system_State next_state;
	bool new_create;
    bool start_record;
    bool start_monitor;
    uint8_t data_from_raw;
	uint8_t send_delay;
    uint8_t now_cnt;
}Log_system;

extern Log_system log_system;
void log_system_auto(void);
void settings_file_check(void);
void send_monitor_variable_packs(void);

void normal_log_file_write(Log_Info_State info_state, uint8_t* strs, uint16_t str_len);
void update_param_data(uint8_t* raw_data, uint8_t data_len);
#endif
